<template>
	<view class="job-card" @click="handleCardClick" :class="{bgColor:bgColor}">
		<view class="job-header">
			<view class="job-date">
				<view class="job-title">
					<view class="job-title-box" v-if="job.date !== '今日'">
						<u-icon name="calendar" size="24" color="var(--u-primary)"></u-icon>
						<text class="date-text">{{ job.date }}</text>
					</view>
					<text class="job-title-text">{{ job.title }}</text>
				</view>
			</view>
			
			<view class="job-salary">{{ job.salary }}</view>
		</view>
		
		<view class="job-location">
			<text>{{ job.location }}</text>
			<text v-if="job.distance" class="job-distance">{{ job.distance }}km</text>
		</view>
		
		<view class="job-tags">
			<view 
				v-for="(tag, tagIndex) in job.tags" 
				:key="tagIndex"
				class="job-tag"
			>
				<text v-if="tag.icon" class="tag-icon">{{ tag.icon }}</text>
				<text class="tag-text">{{ tag.name }}</text>
			</view>
		</view>
		
		<!-- 还招岗位展示 -->
		<view v-if="job.positionName" class="job-positions">
			<text class="positions-label">还招：</text>
			<text class="positions-text">{{ job.positionName }}</text>
		</view>
		
		<u-line color="#F0F0F0" margin="20rpx 0" v-if="showBottomActions"></u-line>

		<view class="job-engagement" v-if="showBottomActions">
			<view class="engagement-left">
				<view class="user-avatars">
					<u-avatar-group :urls="avatarList" size="18" gap="0.4"></u-avatar-group>
				</view>
				<text class="engagement-text">{{ job.engagement }}</text>
			</view>
			<view 
				class="apply-btn" 
				:class="{ 'disabled': job.signup }"
				
			>
			<!-- @click.stop="job.signup? null: handleApplyClick()" -->
				<!-- <text class="apply-text">{{ job.signup ? '已报名' : buttonText }}</text> -->
				<text class="apply-text">一键报名</text>
			</view>
		</view>
	</view>
</template>

<script setup lang="ts">
import { computed } from 'vue'
import { useUserStore } from '@/stores/userStore'

// 定義組件屬性
interface JobTag {
	name: string
	icon?: string
}

interface Job {
	date: string
	title: string
	salary: string
	location: string
	distance?: number | null
	signup?: boolean | null
	positionName?: string
	tags: JobTag[]
	engagement: string
}

interface Props {
	job: Job
	avatarList?: string[]
	buttonText?: string
	showBottomActions?: boolean
	bgColor?: boolean
}

const props = withDefaults(defineProps<Props>(), {
	avatarList: () => [],
	buttonText: '一键报名',
	showBottomActions: true
})

// 使用用户store
const userStore = useUserStore()

// 定義事件
const emit = defineEmits<{
	cardClick: [job: Job]
	applyClick: [job: Job]
}>()

// 處理卡片點擊
const handleCardClick = () => {
	emit('cardClick', props.job)
}

// 處理報名按鈕點擊
const handleApplyClick = () => {
	console.log(999)
	// 检查用户是否已实名认证
	if (!userStore.isVerified.value) {
		uni.showModal({
			title: '提示',
			content: '您还未实名认证，请先完成实名认证后再报名',
			confirmText: '去认证',
			cancelText: '取消',
			success: (res) => {
				if (res.confirm) {
					// 跳转到实名认证页面
					uni.navigateTo({
						url: '/pages/verification/verification'
					})
				}
			}
		})
		return
	}
	
	emit('applyClick', props.job)
}
</script>

<style lang="scss" scoped>
.job-card {
	background-color: #FFFFFF;
	border-radius: 16rpx;
	padding: 30rpx;
	box-sizing: border-box;
	width: 100%;
	overflow: hidden;
	margin: 0 auto;
	margin-bottom: 20rpx;
	box-shadow: 0 2rpx 8rpx rgba(0, 0, 0, 0.1);
}
.bgColor{
	background-color: transparent;
}

.job-header {
	display: flex;
	justify-content: space-between;
	align-items: start;
	margin-bottom: 20rpx;
	
	.job-date {
		width: 70%;
		.date-text {
			font-size: 28rpx;
			color: var(--u-primary);
			font-weight: bold;
			
		}
		.job-title-box{
			display: inline-flex;
			align-items: center;
			background-color: var(--u-primary-light);
			border-radius: 10rpx;
			font-size: 18rpx;
			width: 180rpx;
		}
		.job-title-text{
			vertical-align: 2px;
			margin-left: 10rpx;
		}
	}
	
	.job-salary {
		font-size: 32rpx;
		font-weight: bold;
		color: var(--u-red);
	}
}

.job-title {
	font-size: 32rpx;
	font-weight: bold;
	color: #333333;
	margin-bottom: 15rpx;
	line-height: 1.4;
	
}

.job-location {
	font-size: 26rpx;
	color: #999999;
	margin-bottom: 20rpx;
	display: flex;
	justify-content: space-between;
	align-items: center;
	
	.job-distance {
		font-size: 24rpx;
		color: var(--u-primary);
		font-weight: 500;
		background-color: var(--u-primary-light);
		padding: 4rpx 12rpx;
		border-radius: 12rpx;
	}
}

.job-tags {
	display: flex;
	flex-wrap: wrap;
	gap: 10rpx;
	margin-bottom: 20rpx;
	
	.job-tag {
		display: flex;
		align-items: center;
		padding: 8rpx 16rpx;
		background-color: #F5F5F5;
		border-radius: 10rpx; /* 圓角縮小 */
		
		.tag-icon {
			display: none; /* 移除圖標顯示 */
		}
		
		.tag-text {
			font-size: 22rpx;
			color: #666666;
		}
	}
}

.job-positions {
	display: flex;
	align-items: flex-start;
	margin-bottom: 20rpx;
	
	.positions-label {
		font-size: 24rpx;
		color: #666666;
		margin-right: 8rpx;
		flex-shrink: 0;
	}
	
	.positions-text {
		font-size: 24rpx;
		color: #333333;
		line-height: 1.4;
		flex: 1;
	}
}

.job-engagement {
	display: flex;
	align-items: center;
	justify-content: space-between;
	border-top: 1rpx solid #F2F3F5; /* 更細更淡的分割線 */
	padding-top: 12rpx;
	
	.engagement-left {
		display: flex;
		align-items: center;
		
		.user-avatars {
			display: flex;
			margin-right: 15rpx;
			
			.user-avatar {
				width: 40rpx;
				height: 40rpx;
				border-radius: 50%;
				background-color: #F0F0F0;
				display: flex;
				align-items: center;
				justify-content: center;
				font-size: 20rpx;
				margin-right: -10rpx;
				border: 2rpx solid #FFFFFF;
			}
		}
		
		.engagement-text {
			font-size: 24rpx;
			color: var(--u-primary);
		}
	}
	
	.apply-btn {
		background-color: var(--u-primary);
		border-radius: 20rpx;
		padding: 12rpx 24rpx;
		cursor: pointer;
		transition: all 0.3s ease;
		
		&.disabled {
			background-color: #CCCCCC;
			cursor: not-allowed;
			opacity: 0.6;
		}
		
		.apply-text {
			font-size: 24rpx;
			color: #FFFFFF;
			font-weight: 500;
		}
	}
}
</style> 